<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="租赁人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入租赁人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="userIdNo">
        <el-input
          v-model="queryParams.userIdNo"
          placeholder="请输入租赁人身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['xiaotaoqi:zulin:add']"
        >新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租赁人姓名" align="center" prop="userName" />
      <el-table-column label="租赁人身份证号" align="center" prop="userIdNo" />
      <el-table-column label="机械编号" align="center" prop="machineNo" />
      <el-table-column label="押金" align="center" prop="pledgeAmount" />
      <el-table-column label="付款金额" align="center" prop="payAmount" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
      </el-table-column>
      <el-table-column label="业务员" align="center" prop="salesmanName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="RefreshRight" @click="handleUpdate(scope,$index,scope.row)" v-hasPermi="['xiaotaoqi:zulin:edit']">修改</el-button>
          <el-button link type="primary" icon="CircleClose" @click="handleDelete(scope.row)" v-hasPermi="['xiaotaoqi:zulin:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer v-model="drawerShow" :show-close="false" :title="drawerTittle" size="80%" direction="ttb">

        <el-form :model="formData" label-width="auto" style="max-width: 100%" inline="true">
          <el-row>
            <el-col>
              <el-form-item label="租赁的时间范围">

                <el-date-picker
                    v-model="formData.dateScope"
                    type="datetimerange"
                    start-placeholder="租赁开始时间"
                    end-placeholder="租赁结束时间"
                    format="YYYY-MM-DD HH:mm:ss"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    @change = "dateScopeChange"
                />
              </el-form-item>
            </el-col>

          </el-row>
          <el-row :gutter="24">
            <el-col :span="6">
              <el-form-item label="尊姓大名:">
                <el-input v-model="formData.userName" />
              </el-form-item>
            </el-col >
            <el-col :span="6">
              <el-form-item label="身份证号:">
                <el-input v-model="formData.userIdNo" />
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item label="押金" prop="pledgeAmount">
                <el-input v-model="formData.pledgeAmount" placeholder="请输入押金" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="付款金额" prop="payAmount">
                <el-input v-model="formData.payAmount" placeholder="请输入付款金额" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">

            <el-col :span="6">
              <el-form-item label="机械编号" prop="payAmount">
                <el-select
                    v-model="formData.machineInfoId"
                    filterable
                    remote
                    reserve-keyword
                    placeholder="请输入"
                    remote-show-suffix
                    :remote-method="machineInfoIdRemotMethod"
                    :loading="loading"
                    style="width: 240px"
                    @change="machineInfoRemoteChange"
                >
                  <el-option
                      v-for="item in machineInfoSelList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="业务人员" prop="payAmount">
              <el-select
                  v-model="formData.salesmanId"
                  filterable
                  remote
                  reserve-keyword
                  placeholder="请输入"
                  remote-show-suffix
                  :remote-method="remoteMethod"
                  :loading="loading"
                  style="width: 240px"
                  @change="remoteChange"
              >
                <el-option
                    v-for="item in remotSelectList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

      <template #footer>
        <div style="flex: auto">
          <el-button @click="drawerCancleClick">关闭</el-button>
          <el-button type="primary" @click="drawerConfirmClick">保存</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="zulin">
import * as api from "@/api/xiaotaoqi/zulin/index";
import * as userApi from "@/api/system/user"
import * as machineInfoApi from "@/api/xiaotaoqi/machineInfo/index"
import {reactive,ref,nextTick} from 'vue';
import { ElDrawer,ElMessageBox } from 'element-plus'
import {resetForm} from "@/utils/ruoyi.js";


const { proxy } = getCurrentInstance();
//数据定义
const single=ref( true)
// 非多个禁用
const multiple= ref( true)
// 显示搜索条件
const showSearch= ref( true)
// 总条
const total= ref( 0)
//抽屉

const drawerShow = ref(false)
const drawerTittle = ref('新增')
// 业务员远程下拉框数据源
const remotSelectList= ref([])
// 机械类型下拉框
const machineInfoSelList  = ref([])

// 租赁信息表格数据
const infoList= ref([])
const loading= ref(true)
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  machineInfoId: null,
  userName: null,
  userIdNo: null,
  pledgeAmount: null,
  payAmount: null,
  startTime: null,
  endTime: null,
  salesmanId: null,
  salesmanIdList: [],
  salesmanName: null,
})
// 表单参数
const formData= ref({})

const getList = ()=> {
  loading.value = true;
  api.listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
//远程下拉框
const remoteChange = (e)=>{
  if(formData.value.salesmanIdList!=null && formData.value.salesmanIdList.length>0){
    formData.value.salesmanId = formData.value.salesmanIdList.join(",")
  }
}
const dateScopeChange = (e,v,a)=>{
  formData.value.startTime = null
  formData.value.endTime = null
  if(e!=null){
    formData.value.startTime = e[0]
    formData.value.endTime = e[1]
  }
}
//机械信息远程下拉框搜索事件
const machineInfoIdRemotMethod  =(query)=>{

  if (query) {
    let tmpQuery = {
      machineNo:query
    }
    machineInfoApi.listInfo(tmpQuery).then(res=>{
      if(res.rows.length > 0){
        res.rows.forEach(o=>{
          o.label = o.machineNo
          o.value = o.tableId
        })
      }
      machineInfoSelList.value = res.rows;
    })
    // loading.value = true
    // setTimeout(() => {
    //   loading.value = false
    //   options.value = list.value.filter((item) => {
    //     return item.label.toLowerCase().includes(query.toLowerCase())
    //   })
    // }, 200)
  } else {
    machineInfoSelList.value = []
  }
}
//机械信息远程下拉框改变事件
const machineInfoRemoteChange = ()=>{

}
/**
 *
 * @param query
 */
const remoteMethod = (query) => {

  if (query) {
    let tmpQuery = {
      userName:query
    }
    userApi.listUser(tmpQuery).then(res=>{
      if(res.rows.length > 0){
        res.rows.forEach(o=>{
          o.label = o.userName
              o.value = o.userId
        })
      }
      remotSelectList.value = res.rows;
    })
    // loading.value = true
    // setTimeout(() => {
    //   loading.value = false
    //   options.value = list.value.filter((item) => {
    //     return item.label.toLowerCase().includes(query.toLowerCase())
    //   })
    // }, 200)
  } else {
    remotSelectList.value = []
  }
}
const reset=()=> {
    formData.value.tableId= null,
    formData.value.machineInfoId= null
    formData.value.userName= null
    formData.value.userIdNo= null
    formData.value.pledgeAmount= null
    formData.value.payAmount= null
    formData.value.startTime= null
    formData.value.endTime= null
    formData.value.salesmanId= null
    formData.value.salesmanIdList= []
    formData.value.salesmanName= null
    formData.value.createBy= null
    formData.value.createTime= null
    formData.value.updateBy= null
    formData.value.updateTime= null
    formData.value.remark= null
    formData.value.dateScope= null
}

// /!** 搜索按钮操作 *!/
const handleQuery = ()=> {
  queryParams.value.pageNum = 1;
  getList();
}
// /!** 重置按钮操作 *!/
const resetQuery=()=> {
  resetForm("queryForm");
  handleQuery();
}
const handleSelectionChange = (selection)=> {
  ids.value = selection.map(item => item.tableId)
  single.value = selection.length!==1
  multiple.value = !selection.length
}
// /!** 删除按钮操作 *!/
const handleDelete=(row)=> {
  const tableIds = row.tableId || this.ids;
  proxy.$modal.confirm('是否确认删除租赁信息编号为"' + tableIds + '"的数据项？').then(function() {
    return api.delInfo(tableIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/**
 * 新增按钮
 */
const handleAdd=()=> {
 drawerShow.value = true
  formData.value = {}
}

//抽屉相关事件
const drawerCancleClick =()=>{

  ElMessageBox.confirm('关闭不会保存您的信息！依然确认关闭吗？')
      .then(() => {
        drawerShow.value =false
      })
      .catch(() => {
        // catch error
      })
}
 // 修改按钮操作
const handleUpdate=(s,i,row)=> {
  if(row==null){
    proxy.$message.error("先选择一条数据在点击更新操作");
  }
  console.log(row.value);
  reset()
  api.getInfo(row.tableId).then(response => {
    formData.value = response.data;
    drawerShow.value = true;
    drawerTittle.value = "修改租赁信息";
  });
}
const drawerConfirmClick = ()=>{
  ElMessageBox.confirm('确认保存？').then(()=>{
    let tmpDate = formData.value
    console.log(formData.value)
    api.addInfo(tmpDate).then(res=>{
      proxy.$message.success("保存成功")
      drawerShow.value = false
    })
  })

}
const init=()=>{
  getList()
}

init()
</script>
